﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BF.Model;
using BF.IBLL.IXZCF_WS_ZJBCCLJDSBLLLibrary;
using Base.Core.UnityFactory;

namespace BF.Web.pages.XZCF_WS_ZJBCCLJDSPage
{
	public partial class Edit : BasePage
	{
		private readonly IXZCF_WS_ZJBCCLJDSBLL ibll = IOCFactory.GetIOCResolve<IXZCF_WS_ZJBCCLJDSBLL>();
		protected void Page_Load(object sender, EventArgs e)
		{
			if (!IsPostBack)
			{
				BindContrl();
				if (!IsAdd)
				{
					XZCF_WS_ZJBCCLJDS model = ibll.GetModel(P => P.gSLLJKey == Gkey);
					if (model == null)
						throw new NullReferenceException();
					SetFormValue(model);
				}
				//查看页面
                if (!String.IsNullOrEmpty(Request.QueryString["State"]) && (Request.QueryString["State"] == "View"))
                {
                    btnAdd.Visible = false;
                    btnSave.Visible = false;
                    EnabledControls(this.Form);
                }
			}
		}

		private void BindContrl()
		{
					}

		//带请选择的 下拉列表
		private void InitDLL(DropDownList ddl, string strCode)
		{
			List<dic_Dict> modelList = GetDictList(strCode);
			ddl.AppendDataBoundItems = true;
			ddl.Items.Add(new ListItem("--请选择--", ""));
			for (int i = 0; i < modelList.Count; i++)
			{
				ddl.Items.Add(new ListItem(modelList[i].sName.ToString(), modelList[i].sCode.ToString()));
			}
		}

		//TODO 获取页面上的值
		private XZCF_WS_ZJBCCLJDS GetFormValue(XZCF_WS_ZJBCCLJDS model)
		{
			model = (XZCF_WS_ZJBCCLJDS)WebDataControls.WebUIHelper.GetControlsModel(model, "XZCF_WS_ZJBCCLJDS", this.Form);
			return model;
		}

		private void SetFormValue(XZCF_WS_ZJBCCLJDS model)
		{
			WebDataControls.WebUIHelper.SetControlsValue(model, "XZCF_WS_ZJBCCLJDS", this.Form);
		}

		private void Add()
		{
			XZCF_WS_ZJBCCLJDS model = new XZCF_WS_ZJBCCLJDS() { gKey = Guid.NewGuid() };
			GetFormValue(model);
			model.gSLLJKey = Gkey;
			if (ibll.Add(model))
			{
				MessageBox("新增成功！", "function(e){UpatePage('" + model.gSLLJKey.ToString() + "');}");
			}
			else
			{
				Messages("新增失败！");
			}
		}

		private void Update()
		{
			XZCF_WS_ZJBCCLJDS model = ibll.GetModel(P => P.gSLLJKey == Gkey);
			GetFormValue(model);
			if (ibll.Update())//修改操作
			{
				Messages("修改成功！", this);
			}
			else
			{
				Messages("修改失败！");
			}
		}

		//TODO 新增或者修改事件
		protected void btnSave_Click(object sender, EventArgs e)
		{
			//新增
			if (IsAdd)
			{
				Add();
			}
			else //编辑
			{
				Update();
			}
		}

		private Guid Gkey
		{
			get
			{
				if (!string.IsNullOrEmpty(Request.QueryString["sID"]))
				{
					string skey = Request.QueryString["sID"];
					Guid key = default(Guid);
					Guid.TryParse(skey, out key);
					return key;
				}
				else
				{
					return default(Guid);
				}
			}
		}

		private bool IsAdd
		{
			get { XZCF_WS_ZJBCCLJDS model = ibll.GetModel(P => P.gSLLJKey == Gkey); return model == null; }
		}
	}
}

